package com.palm.hubo2o.ui.fragment;

import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

import com.palm.hubo2o.R;
import com.palm.hubo2o.event.BetEvent;
import com.palm.hubo2o.model.Bet;
import com.palm.hubo2o.util.EnumCode;
import com.palm.palmutils.MathUtils;
import com.palm.palmutils.widget.EasyRecyclerView;

import de.greenrobot.event.EventBus;

/**
 * Created by dest1 on 2015/10/15.
 * 直选
 */
public class D3DirFragment extends MostLotteryFragment {

    private SparseArray<String> baiBalls = new SparseArray<>();
    private SparseArray<String> shiBalls = new SparseArray<>();
    private SparseArray<String> geBalls = new SparseArray<>();

    private final Bet bet = new Bet(EnumCode.gameId.D3);

    @Override
    public void onCreateView2(View view, Bundle savedInstanceState) {
        super.onCreateView2(view, savedInstanceState);
        setupBalls(null, 0, 10, 10, 10);
    }

    @Override
    public boolean onRandomBalls() {
        onClearChoices();
        for (int i = 0; i < 3; i++) {
            mGrid.setItemChecked(getAdapter().transToRawPosition(MathUtils.random(10), i), true);
        }
        return true;
    }

    @Override
    protected void onClearChoices() {
        super.onClearChoices();
        baiBalls.clear();
        shiBalls.clear();
        geBalls.clear();
    }

    @Override
    protected void onSetMarkText(View parent, int groupIndex) {
        TextView title = (TextView) parent.findViewById(R.id.title_mark);
        TextView tip = (TextView) parent.findViewById(R.id.tip_mark);
        if (groupIndex == 0) {
            title.setText("百位");
            tip.setVisibility(View.VISIBLE);
            tip.setText("每位至少选择1个号码");
        } else if (groupIndex == 1) {
            title.setText("十位");
            tip.setVisibility(View.GONE);
        } else {
            title.setText("个位");
            tip.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected int onSetGroupColor(int groupIndex) {
        return red;
    }


    @Override
    protected boolean onBallChecked(EasyRecyclerView parent, boolean checked, int position, String ballText, int index) {
        if (index == 0) {//百位
            if (checked) baiBalls.put(position, ballText);
            else baiBalls.remove(position);
        } else if (index == 1) {//十位
            if (checked) shiBalls.put(position, ballText);
            else shiBalls.remove(position);
        } else {//个位
            if (checked) geBalls.put(position, ballText);
            else geBalls.remove(position);
        }
        combineBalls(baiBalls, shiBalls, geBalls);
        return true;
    }

    private void combineBalls(SparseArray<String> bai, SparseArray<String> shi, SparseArray<String> ge) {
        if (bai.size() >= 1 && shi.size() >= 1 && ge.size() >= 1) {
            bet.getReds().clear();
            for (int a = 0; a < bai.size(); a++)
                for (int b = 0; b < shi.size(); b++)
                    for (int c = 0; c < ge.size(); c++) {
                        bet.getReds().add(bai.valueAt(a) + shi.valueAt(b) + ge.valueAt(c));
                    }
            bet.setWagerCount(bai.size() * shi.size() * ge.size());
            bet.setPlayType(bet.getWagerCount() == 1 ? EnumCode.PlayType.D3_ZXDS : EnumCode.PlayType.D3_ZX);
            EventBus.getDefault().post(new BetEvent(bet));
        } else {
            EventBus.getDefault().post(new BetEvent());
        }
    }

}
